/**
 * Created by ROGSTAR on 2016/11/7.
 */
require.config(window.jshead);
require(['jquery','layer','include/navigator'], function($,layer) {
    layer.config({
        path: '/js/layer/'
    });

    $.ajax({
        type: "POST",
        url: "/user/security/information",
        dataType: "json",
        success: function (data) {

            if (data.security.realnameStatus == 0) {

            } else {
                $('#first').css('display','none');
                $('#second').css('display','block');
                $('#third').css('display','none');
            }
        }
    });
    //$('#passwordNoSee').bind('click',function(){
    //    $('#payPassWord').attr('type','text');
    //    $('#payPasswordCheck').attr('type','text');
    //    $('#passwordSee').show();
    //    $('#passwordNoSee').hide();
    //});
    //$('#passwordSee').bind('click',function(){
    //    $('#payPassWord').attr('type','password');
    //    $('#payPasswordCheck').attr('type','password');
    //    $('#passwordNoSee').show();
    //    $('#passwordSee').hide();
    //});
    $('#forgetNext').bind('click',function(){

        var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        var emailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
        var realName=$('#realName').val();
        var identityNum=$('#identityNum').val();
        var email=$('#email').val();
        var payPassWord=$("#payPassWord").val();
        var payPasswordCheck=$("#payPasswordCheck").val();
        if(realName==''){
            layer.tips("请输入真实姓名",'#realName');
            return;
        }
        if(identityNum==''){
            layer.tips("请输入身份证号",'#identityNum');
            return;
        }
        if(reg.test(identityNum) === false)
        {
            layer.tips("请输入正确的身份证号",'#identityNum');
            return;
        }
        if(email==''){
            layer.tips("请输入邮箱地址",'#email');
            return;
        }
        if(emailReg.test(email) === false)
        {
            layer.tips("请输入正确的邮箱地址",'#email');
            return;
        }
        if(payPassWord=="" ||payPassWord.length<5 ){
            layer.tips("请输入6位数字密码",'#payPassWord');
            return;
        }
        if(payPasswordCheck!=payPassWord){
            layer.tips("2次密码输入不一致",'#payPasswordCheck');
            return;
        }
        var index = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        $.ajax({
            type: "POST",
            url: "/user/security/doRealname",
            data: {
                realName:realName,
                identityNum:identityNum,
                payPassWord: payPassWord,
                payPasswordCheck:payPasswordCheck,
                email:email
            },
            dataType: "json",
            success: function (data) {
                layer.close(index);
                if(data.result=='success'){
                    layer.msg("实名成功");
                    setTimeout(function(){
                        $('#first').css('display','none');
                        $('#second').css('display','block');
                        $('#third').css('display','none');
                    },2000)
                }else{
                    layer.alert(data.messageText);
                }
            }
        });
    });
    //图片验证码
    $('.validImg').click(function() {
        $(".validImg").attr("src", "/sys/ImageServlet?time=" + new Date());
    });
    $('#clickMes').bind('click',function(){
        var bankMobile=$('#bankMobile').val();
        var validNum=$("#code").val();
        var cardNo=$("#cardNo").val();
        var bankName=$("#bankName").val();
        if(cardNo==""){
            layer.tips("请先输入银行卡号",'#cardNo');
            return;
        }
        if(bankName=="" || bankName=='请输入银行卡号' || bankName=='请输入正确的银行卡号'){
            layer.tips("请先输入银行卡号",'#cardNo');
            return;
        }
        if(bankMobile==""){
            layer.tips("请先输入预留手机号",'#bankMobile');
            return;
        }
        if(isSuccess==2){
            layer.tips("暂不支持此银行卡",'#cardNo');
            return;
        }
        $.ajax({
            type: "POST",
            url: "/doTieOnCardSendSms",
            data: {
                mobile:bankMobile,
                pictureCode: validNum
            },
            dataType: "json",
            success: function (data) {
                time($('#clickMes'));
                if(data.result=="success"){
                }else{
                    layer.msg(data.messageText);
                    wait = 0;
                }
            }
        });
    });
    var wait=60;
    function time(o) {
        if (wait == 0) {
            o.removeAttr("disabled");
            o.val('获取验证码');
            o.css("color", '#ffffff');
            o.css("background","#2c97f4");
            $('.validImg').trigger('click')
            wait = 60;
        } else {
            o.attr("disabled", true);
            o.css("color", '#fff');
            o.css("background", '#ddd');
            o.val("重新发送(" + wait + "s)");
            wait--;
            setTimeout(function() {
                    time(o)
                },
                1000)
        }
    }

    $('#forgetNext2').bind('click',function(){
        var reg=/^\d{16}|\d{19}$/;
        var cardNo=$('#cardNo').val();
        var bankName=$('#bankName') .val();//选中的值
        var bankMobile=$('#bankMobile').val();
        var mobileCode=$('#mobileCode').val();
        if(cardNo==''){
            layer.tips("请输入银行卡号",'#cardNo');
            return;
        }
        if(reg.test(cardNo) === false)
        {
            layer.tips("请输入正确的银行卡号",'#cardNo');
            return;
        }
        if(bankName==''){
            layer.tips("请选择开户银行",'#bankName');
            return;
        }
        if(bankMobile==''){
            layer.tips("请输入预留手机号",'#bankMobile');
            return;
        }
        if(mobileCode==""){
            layer.tips("请输入验证码",'#mobileCode');
            return;
        }
        var index = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        $.ajax({
            type: "POST",
            url: "/user/security/tieOnCard",
            data: {
                cardNo:cardNo,
                bankName:bankName,
                bankMobile:bankMobile,
                mobileCode:mobileCode
            },
            dataType: "json",
            success: function (data) {
                layer.close(index);
                if(data.result=='success'){
                    layer.msg("绑卡成功");
                    setTimeout(function(){
                        window.location.href="/user/security?1?6?8";
                    },2000)
                }else{
                    layer.alert(data.messageText);
                    $('#cardNo').val('');
                    $('.phoneNum-input').val('');
                    $('#code').val('')
                    $('#mobileCode').val('')
                    $('.validImg').trigger('click')
                    wait=0;
                }
            }
        });
    })
    // $(".sixDigitPassword span").removeClass("guangbiao");
    // $("#payPassWord").bind("focus",function(){
    //     $(".sixDigitPassword span").addClass("guangbiao");
    //     $(".guangbiao").css({"left": 0});
    // });
    //实名认证密码设置
    $(".i-text.one").bind("focus",function(){
        $(".oneStyle").addClass("guangbiao");
        var inp_v = $(this).val();
        var inp_l = inp_v.length;
        if(inp_l==6){
            inp_l=5;
        }
        // $(this).siblings(".sixDigitPassword").find("i").eq( inp_l ).addClass("active").siblings("i").removeClass("active");
    });
    $(".i-text.one").bind("blur",function(){
        $(".oneStyle").removeClass("guangbiao");

        // $(".sixDigitPassword").find("i").removeClass("active");
        $(".guangbiao").css({"left": 0});
    });
    $(".i-text.one").keyup(function()
    {
        var inp_v = $(this).val();
        var inp_l = inp_v.length;
        //$("p").html( "input的值为：" + inp_v +"; " + "值的长度为:" + inp_l);//测试用
        for( var x = 0; x<=6; x++)
        {
            //$("p").html( inp_l );//测试
            // $(this).siblings(".sixDigitPassword").find("i").eq( inp_l ).addClass("active").siblings("i").removeClass("active");
            $(this).siblings(".sixDigitPassword").find("i").eq( inp_l ).prevAll("i").find("b").css({"display":"block"});
            $(this).siblings(".sixDigitPassword").find("i").eq( inp_l - 1 ).nextAll("i").find("b").css({"display":"none"});
            // $(".sixDigitPassword").find("i").eq(0).removeClass("active");
            $(".guangbiao").css({"left": inp_l * 50});//光标位置
            if( inp_l == 0)
            {
                // $(this).siblings(".sixDigitPassword").find("i").eq( 0 ).addClass("active").siblings("i").removeClass("active");
                $(this).siblings(".sixDigitPassword").find("b").css({"display":"none"});
                $(".guangbiao").css({"left": 0});
            }
            else if( inp_l == 6)
            {
                $(this).siblings(".sixDigitPassword").find("b").css({"display":"block"});
                // $(this).siblings(".sixDigitPassword").find("i").eq(5).addClass("active").siblings("i").removeClass("active");
                //$(this).siblings(".sixDigitPassword").find("i").eq(5).removeClass("active");
                $(".guangbiao").css({"left": 5 * 50});
            }
        }
    });

    //实名认证密码设置
    $(".i-text.two").bind("focus",function(){
        $(".twoStyle").addClass("guangbiao");
        var inp_v = $(this).val();
        var inp_l = inp_v.length;
        if(inp_l==6){
            inp_l=5;
        }
        // $(this).siblings(".sixDigitPassword").find("i").eq( inp_l ).addClass("active").siblings("i").removeClass("active");
    });
    $(".i-text.two").bind("blur",function(){
        $(".twoStyle").removeClass("guangbiao");

        // $(".sixDigitPassword").find("i").removeClass("active");
        $(".guangbiao").css({"left": 0});
    });
    $(".i-text.two").keyup(function()
    {
        var inp_v = $(this).val();
        var inp_l = inp_v.length;
        //$("p").html( "input的值为：" + inp_v +"; " + "值的长度为:" + inp_l);//测试用
        for( var x = 0; x<=6; x++)
        {
            //$("p").html( inp_l );//测试
            // $(this).siblings(".sixDigitPassword").find("i").eq( inp_l ).addClass("active").siblings("i").removeClass("active");
            $(this).siblings(".sixDigitPassword").find("i").eq( inp_l ).prevAll("i").find("b").css({"display":"block"});
            $(this).siblings(".sixDigitPassword").find("i").eq( inp_l - 1 ).nextAll("i").find("b").css({"display":"none"});
            // $(".sixDigitPassword").find("i").eq(0).removeClass("active");
            $(".guangbiao").css({"left": inp_l * 50});//光标位置
            if( inp_l == 0)
            {
                // $(this).siblings(".sixDigitPassword").find("i").eq( 0 ).addClass("active").siblings("i").removeClass("active");
                $(this).siblings(".sixDigitPassword").find("b").css({"display":"none"});
                $(".guangbiao").css({"left": 0});
            }
            else if( inp_l == 6)
            {
                $(this).siblings(".sixDigitPassword").find("b").css({"display":"block"});
                // $(this).siblings(".sixDigitPassword").find("i").eq(5).addClass("active").siblings("i").removeClass("active");
                //$(this).siblings(".sixDigitPassword").find("i").eq(5).removeClass("active");
                $(".guangbiao").css({"left": 5 * 50});
            }
        }
    });

    var isSuccess=2;
    //校验银行卡号正则表达式
    $('#cardNo').bind('blur',function(){
        var reg = /^(\d{16}|\d{19})$/
        var cardNo = $('#cardNo').val();
        if(!reg.test(cardNo)){
            layer.tips('请核对后输入正确的银行卡号','#cardNo');
            $('#bankName').val('请输入正确的银行卡号');
            $("#cardChange").hide();
        }else{
            $.ajax({
                type:'post',
                url:'/account/bank/bankNoJudge',
                data:{
                    bankNo:cardNo,
                },
                dataType:'json',
                success:function(data){
                   if(data.result=='fail'){
                       layer.msg(data.messageText);
                       $('#bankName').val(data.messageText);
                       isSuccess=2;
                   }else{
                       console.log(data.showapi_res_body.bankName.split("（")[0]);
                       if(data.showapi_res_body.bankName.indexOf("（")!= -1){
                           var bankName=data.showapi_res_body.bankName.split("（")[0]+'-'+data.showapi_res_body.cardType;
                       }else if(data.showapi_res_body.bankName.indexOf("-")!= -1){
                           var bankName=data.showapi_res_body.bankName.split("-")[0]+'-'+data.showapi_res_body.cardType;
                       }else{
                           var bankName=data.showapi_res_body.bankName+'-'+data.showapi_res_body.cardType;
                       }
                       $('#bankName').val(bankName);
                       isSuccess=1;
                       if(bankName!="" && bankName!='请输入银行卡号' && bankName!='请输入正确的银行卡号'){
                           $("#cardChange").show();
                           $("#cardNo").attr({"disabled":"disabled"})
                       }
                   }
                }
            })
        }
    })
    $("#cardChange").bind("click", function () {
        $("#cardNo").removeAttr("disabled").focus().val("");
        $("#cardChange").hide();
    })
    //校验身份证
    $('#identityNum').blur(function(){
        var code = $('#identityNum').val();
        if(!code || !/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i.test(code)){
           layer.msg('身份证格式错误')
            return;        }
    })
});